package com.cityfood.controller;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cityfood.common.pojo.CityFoodResult;
import com.cityfood.common.pojo.SimpleProduct;
import com.cityfood.pojo.CfCardDefault;
import com.cityfood.pojo.CfProduct;
import com.cityfood.pojo.CfUserCollect;
import com.cityfood.service.ProductService;
import com.cityfood.service.UserCollectService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

@Controller
public class UserCollectController {
	
	@Autowired
	private UserCollectService collectService;
	
	@Autowired
	private ProductService productService;
	
	@RequestMapping(value="/collect",method=RequestMethod.GET)
	@ResponseBody
	public CityFoodResult getCollectByUserId(@RequestParam Integer userId,@RequestParam(defaultValue="1") Integer currentPage){
		CityFoodResult cityFoodResult = new CityFoodResult();
		
		//5.获取分页信息
		PageInfo<CfUserCollect> collectListPage = collectService.getCollectByUserIdAndPage(userId, currentPage);
				
		if (collectListPage.getList().size()>0) {
			//商品收藏展示包装类集合
			List<SimpleProduct> simpleProductList = new ArrayList<SimpleProduct>();
			for (CfUserCollect uc : collectListPage.getList()) {
				if (uc.getDisplay()!=0) {
					CfProduct product = productService.getProductByProductId(uc.getProductId());
					SimpleProduct cp = new SimpleProduct();
					cp.setFoodId(product.getId());
					cp.setFoodName(product.getName());
					cp.setFoodTitle(product.getSubTitle());
					cp.setPic(product.getMainImage());
					simpleProductList.add(cp);
				}
			}
			
			Map<String,Object> ms = new LinkedHashMap<>();
			ms.put("total", collectListPage.getTotal());
			ms.put("collects", simpleProductList);
			
			cityFoodResult.setCode(200);
			cityFoodResult.setMsg("查询用户的收藏成功");
			cityFoodResult.setData(ms);
		}else{
			cityFoodResult.setCode(201);
			cityFoodResult.setMsg("收藏夹空空的，什么都没有");
			cityFoodResult.setData("{}");
		}
		return cityFoodResult;
	}
	

	@RequestMapping(value="/collect",method=RequestMethod.POST)
	@ResponseBody
	public CityFoodResult insertCollectByUserId(@RequestParam Integer userId,@RequestParam Integer productId){
		CityFoodResult cityFoodResult = new CityFoodResult();
		CfUserCollect collect=new CfUserCollect();
		collect.setUserId(userId);
		collect.setProductId(productId);
		collect.setDisplay((byte)1);
		int i = collectService.insertCollect(collect);
		if (i>0) {
			cityFoodResult.setMsg("添加收藏成功");
			cityFoodResult.setCode(200);
			cityFoodResult.setData(true);
		}else{
			cityFoodResult.setMsg("添加收藏失败");
			cityFoodResult.setCode(201);
			cityFoodResult.setData(false);
		}
		return cityFoodResult;
	}

	
	@RequestMapping(value="/collect",method=RequestMethod.DELETE)
	@ResponseBody
	public CityFoodResult deleteCollectByUserId(@RequestParam Integer userId,@RequestParam String productIds){
		CityFoodResult cityFoodResult = new CityFoodResult();
		String[] strIds = productIds.split(",");
		for (String id : strIds) {
			int i = collectService.deleteCollectByUserId(userId,Integer.parseInt(id));
			if (i<=0){
				cityFoodResult.setMsg("取消收藏失败");
				cityFoodResult.setCode(201);
				cityFoodResult.setData(false);
				return cityFoodResult;
			}
		}
		cityFoodResult.setMsg("取消收藏成功");
		cityFoodResult.setCode(200);
		cityFoodResult.setData(true);
		return cityFoodResult;
	}

}
